﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace gChartSharp
{
    /// <summary>
    /// http://code.google.com/apis/chart/#hor_line_marker
    /// </summary>
    public class RangeMarkerParam : IChartParam
    {

        /// <summary>
        /// The name of the API
        /// </summary>
        public string Name { get { return "chm"; } }


        /// <summary>
        /// A list of RangeMarkers
        /// </summary>
        public List<RangeMarker> Markers 
        {
            get { return mRangeMarkers ?? (mRangeMarkers = new List<RangeMarker>()); }
            set { mRangeMarkers = value; }
        }
        private List<RangeMarker> mRangeMarkers;
        

        /// <summary>
        /// Default constructor
        /// </summary>
        public RangeMarkerParam() { }


        /// <summary>
        /// Constructor that populates the RangeMarkers list
        /// </summary>
        /// <param name="rangeMarkers"></param>
        public RangeMarkerParam(RangeMarker[] rangeMarkers)
        {
            foreach (RangeMarker rangeMarker in rangeMarkers)
                this.Markers.Add(rangeMarker);
        }


        /// <summary>
        /// Simple constructor to add one range marker upon creation
        /// </summary>
        /// <param name="rangeMarker"></param>
        public RangeMarkerParam(RangeMarker rangeMarker)
        {
            this.Markers.Add(rangeMarker);
        }


        /// <summary>
        /// Render the text of the API
        /// </summary>
        /// <returns></returns>
        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("&{0}=", this.Name));

            foreach (RangeMarker rangeMarker in this.Markers)
                sb.Append(String.Format("{0},{1},0,{2},{3}|", (char)rangeMarker.RangeMarkerType, 
                    rangeMarker.Color.ToHTML(), rangeMarker.StartPoint, 
                    rangeMarker.EndPoint));

            sb.Remove(sb.ToString().LastIndexOf('|'), 1);

            return sb.ToString();
        }
    }


    public class RangeMarker
    {
        /// <summary>
        /// The type of marker
        /// </summary>
        public RangeMarkerType RangeMarkerType { get; set; }


        /// <summary>
        /// The color of the marker
        /// </summary>
        public Color Color { get; set; }


        /// <summary>
        /// Starting point - value must be between 0.00 and 1.00
        /// </summary>
        public float StartPoint 
        {
            get { return mStartPoint; }
            set 
            {
                if (value >= 0 && value <= 1)
                    mStartPoint = (float)Math.Round(value, 2);
                else
                    throw new ParamException("StartPoint must be between 0.00 and 1.00");
            } 
        }
        private float mStartPoint;


        /// <summary>
        /// Ending point - value must be between 0.00 and 1.00
        /// </summary>
        public float EndPoint
        {
            get { return mEndPoint; }
            set
            {
                if (value >= 0 && value <= 1)
                    mEndPoint = (float)Math.Round(value, 2);
                else
                    throw new ParamException("EndPoint must be between 0.00 and 1.00");
            }
        }
        private float mEndPoint;


        /// <summary>
        /// Constructor to initialize all properties
        /// </summary>
        /// <param name="rangeMarkerType"></param>
        /// <param name="color"></param>
        /// <param name="startPoint"></param>
        /// <param name="endPoint"></param>
        public RangeMarker(RangeMarkerType rangeMarkerType, Color color, float startPoint, float endPoint)
        {
            this.RangeMarkerType = rangeMarkerType;
            this.Color = color;
            this.StartPoint = startPoint;
            this.EndPoint = endPoint;
        }


        // hide the default constructor
        private RangeMarker() { }
    }


    public enum RangeMarkerType
    {
        Horizontal = 'r',
        Vertical = 'R'
    }
}
